package edu.uwm.universitydb.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import edu.uwm.universitydb.model.Department;
import edu.uwm.universitydb.model.Professor;
import edu.uwm.universitydb.service.ServiceFactory;
import edu.uwm.universitydb.service.dept.DepartmentService;
import edu.uwm.universitydb.service.professor.ProfessorService;
import edu.uwm.universitydb.swing.ProfessorTableModel;


public class ProfessorMaint extends JPanel {
	
	public ProfessorMaint(ProfessorTableModel dataService) {
		_dataModel = dataService;
		this.setLayout(new BorderLayout());
		
		JPanel titlePanel = new JPanel();
		titlePanel.setLayout(new BorderLayout());
		titlePanel.setBackground(Color.WHITE);
		titlePanel.setBorder(BorderFactory.createMatteBorder(0, 0, 16, 0, Color.WHITE));
		JLabel title = new JLabel("Professor Maintenance");
		title.setFont(new Font(title.getFont().getName(), title.getFont().getStyle(), 18));
		titlePanel.add(title);

		this.add(titlePanel, BorderLayout.NORTH);
		this.add(this.createAppView(), BorderLayout.CENTER);
		
	}
	
	private JPanel createAppView() {
		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		
		//Create New, Edit, Delete buttons panel
		JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 5));
		_editButton = new JButton(ProfessorMaint.EDITBUTTON);
		_editButton.setIcon(new ImageIcon("res/edit.png"));
		_editButton.setName(ProfessorMaint.EDITBUTTON);
		
		_deleteButton = new JButton(ProfessorMaint.DELBUTTON);
		_deleteButton.setIcon(new ImageIcon("res/remove.png"));
		_deleteButton.setName(ProfessorMaint.DELBUTTON);
		
		_addButton = new JButton(ProfessorMaint.ADDBUTTON);
		_addButton.setIcon(new ImageIcon("res/add.png"));
		_addButton.setName(ProfessorMaint.ADDBUTTON);
		
		btnPanel.setBackground(Color.WHITE);
		btnPanel.add(_addButton);
		btnPanel.add(_editButton);
		btnPanel.add(_deleteButton);
		panel.add(btnPanel);

		//Create Table
		_datatable = new JTable(_dataModel);
		_datatable.setAlignmentX(Component.CENTER_ALIGNMENT);
		_datatable.setAutoCreateRowSorter(true);
		_datatable.getColumnModel().getColumn(0).setMaxWidth(110);
		_datatable.getColumnModel().getColumn(0).setMinWidth(110);
		_datatable.getColumnModel().getColumn(2).setMaxWidth(50);
		_datatable.getTableHeader().setReorderingAllowed(false);

		JScrollPane tblPane = new JScrollPane(_datatable);
		panel.add(tblPane);
		
		panel.add(Box.createRigidArea(new Dimension(0,16)));
		return panel;
	}
	
	public JPanel showMaintView(String id, String name, int age, String rank, String speciality, Department dept) {
		JPanel panel = new JPanel(new GridLayout(0, 1));

		// SSN
		panel.add(new JLabel("SSN"));
		JTextField ssnField = new JTextField(id);
		ssnField.setName(SSNFIELD);
		ssnField.setEditable(false);
		panel.add(ssnField);
		
		//Name
		panel.add(new JLabel("Name"));
		JTextField nameField = new JTextField(name);
		nameField.setName(NAMEFIELD);
		panel.add(nameField);
		
		//Age
		panel.add(new JLabel("Age"));
		JTextField ageField = new JTextField(Integer.toString(age));
		ageField.setName(AGEFIELD);
		panel.add(ageField);
		
		//Rank
		panel.add(new JLabel("rank"));
		JTextField rankField = new JTextField(rank);
		rankField.setName(RANKFIELD);
		panel.add(rankField);
		
		//Specialty
		panel.add(new JLabel("Specialty"));
		JTextField specialityField = new JTextField(speciality);
		specialityField.setName(SPCFIELD);
		panel.add(specialityField);
		
		//Department
		panel.add(new JLabel("Department"));
		DepartmentService deptService = ServiceFactory.getInstance().getDepartmentService();
		List<Department> deptList = deptService.getDepartments();
		JComboBox<Department> deptComboBox = new JComboBox<Department>(deptList.toArray(new Department[deptList.size()]));
		deptComboBox.setName(DEPTFIELD);
		deptComboBox.setSelectedItem(dept);
		deptComboBox.setEnabled(false);
		panel.add(deptComboBox);
		
		//Display Edit Panel
	    return panel;
	}
	
	public JPanel showNewView() {
		JPanel panel = new JPanel(new GridLayout(0, 1));

		// SSN
		panel.add(new JLabel("SSN"));
		JTextField ssn = new JTextField();
		ssn.setName(SSNFIELD);
		panel.add(ssn);
		
		//Name
		panel.add(new JLabel("Name"));
		JTextField name = new JTextField();
		name.setName(NAMEFIELD);
		panel.add(name);
		
		//Age
		panel.add(new JLabel("Age"));
		JTextField age = new JTextField();
		age.setName(AGEFIELD);
		panel.add(age);
		
		//Rank
		panel.add(new JLabel("Rank"));
		JTextField rank = new JTextField();
		rank.setName(RANKFIELD);
		panel.add(rank);
		
		//Specialty
		panel.add(new JLabel("Specialty"));
		JTextField speciality = new JTextField();
		speciality.setName(SPCFIELD);
		panel.add(speciality);
		
		//Department
		panel.add(new JLabel("Department"));
		DepartmentService deptService = ServiceFactory.getInstance().getDepartmentService();
		List<Department> deptList = deptService.getDepartments();
		JComboBox<Department> deptComboBox = new JComboBox<Department>(deptList.toArray(new Department[deptList.size()]));
		deptComboBox.setName(DEPTFIELD);
		deptComboBox.setSelectedIndex(-1);
		panel.add(deptComboBox);
		
		return panel;
	}
	
	
	public JButton getAddButton() {
		return _addButton;
	}
	
	public JButton getEditButton() {
		return _editButton;
	}
	
	public JButton getDelButton() {
		return _deleteButton;
	}
	
	public JTable getDataTable() {
		return _datatable;
	}
	
	public void refreshTable() {
		_dataModel.refreshData();
		_datatable.revalidate();
	    _datatable.repaint();	
	}
	
	private JButton _editButton;
	private JButton _deleteButton;
	private JButton _addButton;
	private JTable _datatable;
	
	private ProfessorTableModel _dataModel;
	
	public static final String EDITBUTTON = "Edit";
	public static final String DELBUTTON = "Delete";
	public static final String ADDBUTTON = "New";

	public static final String SSNFIELD = "SSN";
	public static final String NAMEFIELD = "NAME";
	public static final String AGEFIELD = "AGE";
	public static final String RANKFIELD = "RANK";
	public static final String SPCFIELD = "SPECIALITY";
	public static final String DEPTFIELD = "DEPARTMENT";

	private static final long serialVersionUID = 1957186212396617080L;

}
